package android.longs.study.upgrade.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import android.longs.study.home.servlet.model.MobleHomeModel;
import android.longs.study.upgrade.model.UpgradeModel;
import android.longs.study.utils.H3Utils;
import android.longs.study.utils.HBUtils;

public class UpgradeDaoImp  implements UpgradeDao {

	@Override
	public List<UpgradeModel> queryAll() {
		
		
		Session session = H3Utils.getCurrentSession();
		Criteria createCriteria = session.createCriteria(UpgradeModel.class);
		List<UpgradeModel> list = createCriteria.list();
		
		
		return list;
	}

	@Override
	public void addUpgradeModel(UpgradeModel model) {
		
		if (model==null) {
			return;
		}
		Session session = H3Utils.getCurrentSession();
		//**** 操作
		session.save(model);

	}

	@Override
	public UpgradeModel queryLast() {
		List<UpgradeModel> queryAll = this.queryAll();
		UpgradeModel upgradeModel = null;
		if (queryAll!=null) {
			upgradeModel= queryAll.get(queryAll.size()-1);
		}
		
		
		return upgradeModel;
	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteOne(String version) {
		 String hql;
		 Session session = HBUtils.openSession();
		
		 
		//4 开启事务
			Transaction transaction = session.beginTransaction();
			
		 hql = "delete UpgradeModel where version = :oldName";
		  int deleteEntities = session.createQuery(hql)
		      .setString("oldName",version)
		      .executeUpdate();
		  
		  transaction.commit();
		
	}

}
